package com.nsgf.library.utils;
/*
 * 文 件 名:  DialogUtil.java
 * 版    权:  Copyright (c) 2006-2014 ICS&S Inc,  All rights reserved
 * 描    述:  <描述>
 * 修 改 人:  Desmond
 * 修改时间:  2014-10-29
 * 修改版本号:  <版本编号>
 * 修改履历:  <修改内容>
 */

import android.annotation.SuppressLint;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.content.Context;
import android.content.DialogInterface.OnClickListener;
import androidx.appcompat.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import com.nsgf.library.R;

import java.util.Calendar;

/**
 * dialog工具
 *
 * @author Desmond
 * @version [版本号, 2024-10-29]
 */
@SuppressLint("NewApi")
public class DialogUtil {

    private Context context;

    private LayoutInflater inflater;

    public DialogUtil(Context context) {
        this.context = context;
        this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }


    /**
     * 获取等待提示框
     *
     * @param text 显示的文字，为null显示“请稍后...” void
     * @author Desmond 2014-10-29 下午4:33:35
     */
    public Dialog getLoadingDialog(String text) {
        View v = inflater.inflate(R.layout.dialog_loading, null);
        TextView tipTextView = (TextView) v.findViewById(R.id.loadingmsg);
        if (text != null) {
            tipTextView.setText(text);
            tipTextView.setVisibility(View.VISIBLE);
        } else
            tipTextView.setVisibility(View.GONE);
        CustomDialog dialog = new CustomDialog(context, v, R.style.dialog_style);
        dialog.setCancelable(true);
        return dialog;
    }

    /**
     * 显示文字
     *
     * @param text
     */
    public void setContent(Dialog dialog, String text) {
//        if (text == null){
//            text = "加载中，请稍候...";
//        }
        TextView tipTextView = (TextView) dialog.getWindow().getDecorView().findViewById(R.id.loadingmsg);
        if (text != null) {
            tipTextView.setText(text);
            tipTextView.setVisibility(View.VISIBLE);
        } else
            tipTextView.setVisibility(View.GONE);
    }

    /**
     * 显示确认/取消dialog
     *
     * @param title       标题
     * @param msg         内容
     * @param okClick     确认按钮点击事件
     * @param cancelClick 取消按钮点击事件 void
     * @author Desmond 2014-10-29 下午4:41:16
     */
    public void showConfirmDialog(String title, CharSequence msg, OnClickListener okClick, OnClickListener cancelClick) {
        showConfirmDialog(title, msg, "确定", "取消", okClick, cancelClick);
    }

    /**
     * 显示确认/取消dialog
     *
     * @param title       标题
     * @param msg         内容
     * @param okClick     确认按钮点击事件
     * @param cancelClick 取消按钮点击事件 void
     * @author adminstrotor 2014-10-14 下午4:41:16
     */
    public void showConfirmDialog(String title, CharSequence msg, String positive, String negative, OnClickListener okClick, OnClickListener cancelClick) {
        showConfirmDialog(title, msg, positive, negative, false, okClick, cancelClick);
    }

    /**
     * @param title
     * @param msg
     * @param positive
     * @param negative
     * @param cancelable
     * @param okClick
     * @param cancelClick
     */
    public void showConfirmDialog(String title, CharSequence msg, String positive, String negative, boolean cancelable, OnClickListener okClick, OnClickListener cancelClick) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle(title);
        builder.setMessage(msg);
        builder.setPositiveButton(positive, okClick);
        builder.setNegativeButton(negative, cancelClick);
        AlertDialog dialog = builder.create();
        dialog.setCancelable(cancelable);
        dialog.show();

    }

    /**
     * 警告框
     *
     * @param msg
     * @param callback
     */
    public void alert(String msg, OnClickListener callback) {
        alert("", msg, "确定", callback);
    }

    public void alert(String title, String msg, String positive, OnClickListener callback) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle(title);
        builder.setMessage(msg);
        builder.setPositiveButton(positive, callback);
        AlertDialog dialog = builder.create();
        dialog.setCancelable(false);
        dialog.show();
    }

    public void alert(String title, String msg, String positive, String negative, OnClickListener callback) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        if (!StringUtil.isEmpty(title))
            builder.setTitle(title);
        if (!StringUtil.isEmpty(msg))
            builder.setMessage(msg);
        if (!StringUtil.isEmpty(positive))
            builder.setPositiveButton(positive, callback);
        if (!StringUtil.isEmpty(negative))
            builder.setNegativeButton(negative, callback);
        AlertDialog dialog = builder.create();
        dialog.setCancelable(false);
        dialog.show();
    }

    /**
     * 列表
     *
     * @param src
     * @param callback
     */
    public void showListDialog(String[] src, OnClickListener callback) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle("");
        builder.setItems(src, callback);
        builder.setNegativeButton("关闭", null);
        AlertDialog dialog = builder.create();
        dialog.setCancelable(false);
        dialog.show();
//        Window window = dialog.getWindow();
//        WindowManager.LayoutParams lp = window.getAttributes();
//        lp.width = (int)(SysUtil.getScreenWidth(context));
//        lp.gravity = Gravity.BOTTOM;
//        window.setAttributes(lp);
//        window.setWindowAnimations(R.style.dialog_animation);
    }

    /**
     * 日期dialog
     *
     * @param y        默认显示的年份
     * @param m        默认显示的月份
     * @param d        默认显示的日
     * @param minDate  最小日期（时间戳），小于或等于0就失效
     * @param maxDate  最大日期（时间戳），小于或等于0就失效
     * @param callBack void
     */
    public void showDateDialog(int y, int m, int d, long minDate, long maxDate, OnDateSetListener callBack) {
        final DatePickerDialog dateDialog = new DatePickerDialog(context, android.app.AlertDialog.THEME_HOLO_LIGHT, callBack, y, m, d);
        if (minDate > 0)
            dateDialog.getDatePicker().setMinDate(minDate);
        if (maxDate > 0)
            dateDialog.getDatePicker().setMaxDate(maxDate);
        dateDialog.show();
    }

    /**
     * 弹出生日选择
     *
     * @param callBack void
     * @author Desmond 2016-3-13 下午3:07:29
     */
    public void showBirthdayDateDialog(Calendar cal, OnDateSetListener callBack) {
        showDateDialog(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), 0, Calendar.getInstance().getTimeInMillis(), callBack);
    }

    public void showTimeDialog(int h, int m, OnTimeSetListener callBack) {
        final TimePickerDialog dateDialog = new TimePickerDialog(context, android.app.AlertDialog.THEME_HOLO_LIGHT, callBack, h, m, true);
        dateDialog.show();
    }

    /**
     * 简单自定义dialog
     *
     * @param v
     * @param title
     * @param msg
     * @param okClick
     * @param cancelClick
     */
    public void showCustomDialog(View v, String title, String msg, OnClickListener okClick, OnClickListener cancelClick) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context, android.app.AlertDialog.THEME_HOLO_LIGHT);
        builder.setTitle(title);
        builder.setMessage(msg);
        builder.setView(v);
        builder.setPositiveButton("确定", okClick);
        builder.setNegativeButton("取消", cancelClick);
        AlertDialog dialog = builder.create();
        dialog.setCancelable(false);
        dialog.show();
    }


}
